﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NAnt.Core;
using NAnt.Core.Attributes;
using System.Xml.Serialization;
using System.IO;
using UofCASE.AgileProductLinerDSL.Confeaturator;
using UofCASE.GreenPepe.Runner;
using UofCASE.AgileProductLinerDSL;
using System.Diagnostics;
using Microsoft.VisualStudio.IntelliTrace;
using UofCASE.AgileProductLinerDSL.Nant.Tasks;
using Microsoft.VisualStudio.Samples;
using UofCASE.AgileProductLinerDSL.Nant.Tasks.Models;

namespace UofCASE.AgileProductLinerDSL.Nant
{
    public class AspenTest
    {
        
        public String ResultsDir { get {
            return ProductLinerDir + "/Platforms/" + Platform + "/Results";
        } }

        public string SolutionRoot { get; set; }
        public string AssemblyPath { get; set; }
        public string ProductLinerDir { get; set; }
        public string Platform { get; set; }
        public string CollectionPlan { get; set; }

        public string PlatformDir
        {
            get{
                return ProductLinerDir + "/Platforms/" + Platform;
            }
        }
        
        public AspenTest()
        {
            SolutionRoot = ".";
        }

        public void ExecuteTask()
        {
            ProductLinerSet set = new ProductLinerSet(SolutionRoot, ProductLinerDir, AssemblyPath, CollectionPlan);
            String[] configurations = Directory.GetFiles(PlatformDir);
            Platform p = new Platform(Platform);
            set.AddPlatform(p, new List<String>(configurations));
            set.RunTestsFor(p);
            
            String resultSummary = p.ToXml();
            using (TextWriter writer = new StreamWriter(p.PlatformDir() + "/Results/results.xml"))
            {
                writer.WriteLine(resultSummary);
            }
        }
    }

    class IntellitraceGreenPepeRunner : GreenPepeRunner
    {
        FileInfo logFile;
        String collectionPlan { get; set; }

        public IntellitraceGreenPepeRunner(FileInfo logFile, String collectionPlan)
        {
            this.logFile = logFile;
            this.collectionPlan = collectionPlan;
        }

        protected override ProcessStartInfo getCommand(String[] args)
        {
            ProcessStartInfo parentInfo = base.getCommand(args);


            StringBuilder arguments = new StringBuilder();
            arguments.Append("launch /f:\"").Append(logFile.FullName).Append("\" /cp:\"").Append(collectionPlan).Append("\" ");
            arguments.Append(System.Environment.GetEnvironmentVariable("GREENPEPPER_HOME")).Append("/").Append(parentInfo.FileName);
            arguments.Append(" ").Append(parentInfo.Arguments);

            ProcessStartInfo info = new ProcessStartInfo("intellitrace.exe", arguments.ToString());
            info.RedirectStandardError = parentInfo.RedirectStandardError;
            info.RedirectStandardOutput = parentInfo.RedirectStandardOutput;
            info.CreateNoWindow = parentInfo.CreateNoWindow;
            info.UseShellExecute = parentInfo.UseShellExecute;
            return info;
        }
    }
}
